home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import string
- import re
- import subprocess
- import gettext
- import os.path as os
- from gettext import gettext as _
- from xml.etree.ElementTree import ElementTree
-
- class LocaleInfo(object):
- ''' class with handy functions to parse the locale information '''
- environments = [
- '/etc/default/locale',
- '/etc/environment']
-
- def __init__(self, languagelist_file):
- self._lang = { }
- self._country = { }
- self._languagelist = { }
- et = ElementTree(file = '/usr/share/xml/iso-codes/iso_639.xml')
- it = et.getiterator('iso_639_entry')
- for elm in it:
- lang = elm.attrib['name']
- if elm.attrib.has_key('iso_639_1_code'):
- code = elm.attrib['iso_639_1_code']
- else:
- code = elm.attrib['iso_639_2T_code']
- self._lang[code] = lang
-
- et = ElementTree(file = '/usr/share/xml/iso-codes/iso_639_3.xml')
- it = et.getiterator('iso_639_3_entry')
- for elm in it:
- lang = elm.attrib['name']
- code = elm.attrib['id']
- if not self._lang.has_key(code):
- self._lang[code] = lang
- continue
-
- et = ElementTree(file = '/usr/share/xml/iso-codes/iso_3166.xml')
- it = et.getiterator('iso_3166_entry')
- for elm in it:
- if elm.attrib.has_key('common_name'):
- descr = elm.attrib['common_name']
- else:
- descr = elm.attrib['name']
- if elm.attrib.has_key('alpha_2_code'):
- code = elm.attrib['alpha_2_code']
- else:
- code = elm.attrib['alpha_3_code']
- self._country[code] = descr
-
- for line in open(languagelist_file):
- tmp = line.strip()
- if tmp.startswith('#') or tmp == '':
- continue
-
- w = tmp.split(';')
- localeenv = w[6].split(':')
- self._languagelist[localeenv[0]] = '%s' % w[6]
-
-
-
- def lang(self, code):
- ''' map language code to language name '''
- if self._lang.has_key(code):
- return self._lang[code]
- return ''
-
-
- def country(self, code):
- ''' map country code to country name'''
- if self._country.has_key(code):
- return self._country[code]
- return ''
-
-
- def generated_locales(self):
- ''' return a list of locales available on the system
- (running locale -a) '''
- locales = []
- p = subprocess.Popen([
- 'locale',
- '-a'], stdout = subprocess.PIPE)
- for line in string.split(p.communicate()[0], '\n'):
- tmp = line.strip()
- if tmp.startswith('#') and tmp == '' and tmp == 'C' or tmp == 'POSIX':
- continue
-
- locale = string.split(tmp)[0]
- locale = string.split(locale, '.')[0]
- locale = string.split(locale, '@')[0]
- if locale not in locales:
- locales.append(locale)
- continue
-
- return locales
-
-
- def translate_language(self, lang):
- '''return translated language'''
- lang_name = gettext.dgettext('iso_639', self._lang[lang])
- if lang_name == self._lang[lang]:
- lang_name = gettext.dgettext('iso_639_3', self._lang[lang])
-
- return lang_name
-
-
- def translate_locale(self, locale):
- '''
- return translated language and country of the given
- locale into the given locale, e.g.
- (Deutsch, Deutschland) for de_DE
- '''
- (lang, country) = string.split(locale, '_')
- current_language = None
- if 'LANGUAGE' in os.environ:
- current_language = os.environ['LANGUAGE']
-
- os.environ['LANGUAGE'] = locale
- lang_name = self.translate_language(lang)
- country_name = gettext.dgettext('iso_3166', self._country[country])
- if current_language:
- os.environ['LANGUAGE'] = current_language
-
- return (lang_name, country_name)
-
-
- def translate(self, locale):
- ''' get a locale code and output a human readable name '''
- if '_' in locale:
- (lang, country) = string.split(locale, '_')
- (lang_name, country_name) = self.translate_locale(locale)
- l = (filter,)((lambda k: k.startswith(lang + '_')), self.generated_locales())
- if len(l) > 1:
- mycountry = self.country(country)
- if mycountry:
- return '%s (%s)' % (lang_name, country_name)
- return lang_name
- len(l) > 1
- return lang_name
- '_' in locale
- return self.translate_language(locale)
-
-
- def makeEnvString(self, code):
- ''' input is a language code, output a string that can be put in
- the LANGUAGE enviroment variable.
- E.g: en_DK -> en_DK:en
- '''
- if self._languagelist.has_key(code):
- return self._languagelist[code]
- if '_' not in code:
- return code
- (lang, region) = string.split(code, '_')
- return '%s:%s' % (code, lang)
-
-
- def getDefaultLanguage(self):
- ''' returns the current default language (e.g. zh_CN) '''
- for environment in self.environments:
- if not os.path.exists(environment):
- continue
-
- for line in open(environment).readlines():
- line = line.strip()
- if line.startswith('LANGUAGE='):
- (key, value) = line.split('=')
- value = value.strip('"')
- return value.split(':')[0]
-
- for line in open(environment).readlines():
- match = re.match('LANG="([a-zA-Z_]*).*"$', line)
- if match:
- return match.group(1)
-
-
-
-
- if __name__ == '__main__':
- datadir = '/usr/share/language-selector/'
- li = LocaleInfo('%s/data/languages' % datadir, '%s/data/countries' % datadir, '%s/data/languagelist' % datadir)
- print "default: '%s'" % li.getDefaultLanguage()
- print li._lang
- print li._country
- print li._languagelist
- print li.generated_locales()
-
-